package com.dianban.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dianban.utils.http.HttpClient;
import com.dianban.utils.http.HttpMethod;
import com.dianban.utils.http.HttpParamers;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TencentMapUtils {
    public static final Logger logger = LoggerFactory.getLogger(TencentMapUtils.class);
    //public static final String KEY = "7OABZ-ZCJCS-LX6OT-65UAO-2NAWS-TIB72";
    public static final String KEY = "2ODBZ-C2B36-DK6SD-ENQ5Q-HF2QK-PVFJS";

    // 获取距离计算
    public static JSONObject matrix(String from, String to) {
        JSONObject jsonObject = new JSONObject();
        try {
            String url = "https://apis.map.qq.com/ws/distance/v1/matrix/?mode=walking&from=" + from + "&to=" + to + "&key=" + KEY + "&output=json";
            HttpParamers httpParamers = new HttpParamers(HttpMethod.GET);
            httpParamers.setJsonParamer();
            String resultService = HttpClient.doService(url, httpParamers, null);
            if (StringUtils.isNotBlank(resultService)) {
                jsonObject = JSONObject.parseObject(resultService);
                JSONObject result = JSONObject.parseObject(JSONObject.toJSONString(jsonObject.get("result")));
                if (result != null) {
                    JSONArray rows = JSONArray.parseArray(JSONObject.toJSONString(result.get("rows")));
                    if (rows != null) {
                        JSONObject row = JSONObject.parseObject(JSONObject.toJSONString(rows.get(0)));
                        if (row != null) {
                            JSONArray elements = JSONArray.parseArray(JSONObject.toJSONString(row.get("elements")));
                            if (elements != null) {
                                jsonObject = JSONObject.parseObject(JSONObject.toJSONString(elements.get(0)));
                            }
                        }
                    }
                }
            }
        } catch (Throwable e) {
            logger.error("TencentMapUtils matrix : error - {}", e.getMessage());
        }
        return jsonObject;
    }

    // 根据ip定位
    public static JSONObject location(String ip) {
        JSONObject jsonObject = new JSONObject();
        try {
            String url = "https://apis.map.qq.com/ws/location/v1/ip?ip=" + ip + "&key=" + KEY + "&output=json";
            HttpParamers httpParamers = new HttpParamers(HttpMethod.GET);
            httpParamers.setJsonParamer();
            String resultService = HttpClient.doService(url, httpParamers, null);
            if (StringUtils.isNotBlank(resultService)) {
                jsonObject = JSONObject.parseObject(resultService);
            }
        } catch (Throwable e) {
            logger.error("TencentMapUtils location : error - {}", e.getMessage());
        }
        return jsonObject;
    }

    // 根据经纬度定位
    public static JSONObject getLocation(String location) {
        JSONObject jsonObject = new JSONObject();
        try {
            String url = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + location + "&get_poi=0&key=" + KEY + "&output=json";
            HttpParamers httpParamers = new HttpParamers(HttpMethod.GET);
            httpParamers.setJsonParamer();
            String resultService = HttpClient.doService(url, httpParamers, null);
            if (StringUtils.isNotBlank(resultService)) {
                jsonObject = JSONObject.parseObject(resultService);
            }
        } catch (Throwable e) {
            logger.error("TencentMapUtils getLocation : error - {}", e.getMessage());
        }
        return jsonObject;
    }

    // 百度经纬度转换
    public static JSONObject baiduTranslate(String from) {
        JSONObject jsonObject = new JSONObject();
        try {
            String url = "https://apis.map.qq.com/ws/coord/v1/translate?locations=" + from + "&type=3&key=" + KEY;
            HttpParamers httpParamers = new HttpParamers(HttpMethod.GET);
            httpParamers.setJsonParamer();
            String resultService = HttpClient.doService(url, httpParamers, null);
            if (StringUtils.isNotBlank(resultService)) {
                jsonObject = JSONObject.parseObject(resultService);
                if (jsonObject != null) {
                    JSONArray rows = JSONArray.parseArray(JSONObject.toJSONString(jsonObject.get("locations")));
                    if (rows != null) {
                        JSONObject row = JSONObject.parseObject(JSONObject.toJSONString(rows.get(0)));
                        if (row != null) {
                            jsonObject = row;
                        }
                    }
                }
            }
        } catch (Throwable e) {
            logger.error("TencentMapUtils translate : error - {}", e.getMessage());
        }
        return jsonObject;
    }

    public static void main(String[] args) {
        //{"locations":[{"lat":22.821731,"lng":108.384295}],"message":"转换成功","status":0}
        //System.out.println(baiduTranslate("25.800735,113.032129"));
        //{"message":"query ok","request_id":"5d8b2e86-960b-11ec-8618-525400cd74da","result":{"ad_info":{"adcode":"110108","city":"北京市","city_code":"156110000","district":"海淀区","location":{"lat":39.911358,"lng":116.334602},"name":"中国,北京市,北京市,海淀区","nation":"中国","nation_code":"156","province":"北京市"},"address":"北京市海淀区北四环西路66号","address_component":{"city":"北京市","district":"海淀区","nation":"中国","province":"北京市","street":"北四环西路","street_number":"北四环西路66号"},"address_reference":{"business_area":{"_dir_desc":"内","_distance":0,"id":"14178584199053362783","location":{"lat":39.980598,"lng":116.310997},"title":"中关村"},"crossroad":{"_dir_desc":"北","_distance":185.8,"id":"529979","location":{"lat":39.982498,"lng":116.30809},"title":"海淀大街/彩和坊路(路口)"},"famous_area":{"_dir_desc":"内","_distance":0,"id":"14178584199053362783","location":{"lat":39.980598,"lng":116.310997},"title":"中关村"},"landmark_l2":{"_dir_desc":"内","_distance":0,"id":"3629720141162880123","location":{"lat":39.984253,"lng":116.307472},"title":"中国技术交易大厦"},"street":{"_dir_desc":"西","_distance":46.6,"id":"9217092216709107946","location":{"lat":39.9804,"lng":116.308311},"title":"彩和坊路"},"street_number":{"_dir_desc":"","_distance":47.4,"id":"595672509379194165901290","location":{"lat":39.984089,"lng":116.308037},"title":"北四环西路66号"},"town":{"_dir_desc":"内","_distance":0,"id":"110108012","location":{"lat":39.990639,"lng":116.303162},"title":"海淀街道"}},"formatted_addresses":{"recommend":"海淀区中关村中国技术交易大厦(彩和坊路)","rough":"海淀区中关村中国技术交易大厦(彩和坊路)"},"location":{"lat":39.984154,"lng":116.30749}},"status":0}
        //System.out.println(getLocation("39.984154,116.307490"));
        //https://apis.map.qq.com/ws/distance/v1/matrix/?mode=driving&from=39.984092,116.306934;40.007763,116.353798&to=39.981987,116.362896;39.949227,116.394310&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77
        System.out.println(matrix("28.11266,112.9834", "25.794489,113.025691"));
    }
}
